import requests
import json
from pyecharts import Bar

url='https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=chinaDayList,chinaDayAddList,nowConfirmStatis,provinceCompare'
params={'modules':'chinaDayList,chinaDayAddList,nowConfirmStatis,provinceCompare'}
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41'}
r = requests.get(url,params=params,headers = headers)

j = json.loads(r.text)
res = j['data']['chinaDayList']

date=[]  #用于存储时间的列表
confirm=[] #用于存储确诊的人的列表
heal=[]   #用于存储治愈人数的列表
dead=[]  #用于存储死亡人数的列表
#通过循环将res类中的数据分别放入不同的列表，便于后续生成柱状图
for i in range(0,7):
    date.append(res[len(res)-(7-i)]['date'])
    confirm.append(res[len(res)-(7-i)]['confirm'])
    heal.append(res[len(res)-(7-i)]['heal'])
    dead.append(res[len(res)-(7-i)]['dead'])

bar = Bar("柱状图", "2021年6月近7天疫情状况")
bar.add("确诊人数", date, confirm)
bar.add("治愈人数", date, heal)
bar.add("死亡人数", date, dead)

bar.render()